package net.yxsoft.controller.pc.sysmgt;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.jfinal.aop.Clear;
import com.jfinal.kit.PropKit;
import net.yxsoft.controller.pc.sysmgt.common.AuthController;
import net.yxsoft.kit.FlashcatKit;
import net.yxsoft.service.MachineMonitorService;
import org.apache.commons.lang.StringUtils;

import java.util.HashSet;
import java.util.Set;

public class MachineMonitorController extends AuthController {
    private static final MachineMonitorService asr =MachineMonitorService.INSTANCE;
    /**
     * 返回前端页面
    * */
    @Clear
    public void index() {
        String ident = getPara("ident");
        set("mrIdent",ident);//返回登入标识
        String sourceId = getPara("sourceId");
        if(StringUtils.isBlank(sourceId)){
            String DatasoureId= PropKit.get("flashcat.machineSourceId");
            set("sourceId",DatasoureId);//返回数据源id
        }else {
            set("sourceId",sourceId);//返回数据源id
        }
        JsonNode jsonNode = FlashcatKit.flashcatLogin();
        if(jsonNode!=null&&"".equals(jsonNode.get("err").asText())){
            JsonNode machineList = FlashcatKit.machineList(jsonNode.get("dat").get("access_token").asText());
            if(machineList!=null&&"".equals(machineList.get("err").asText())){
                set("machineList",machineList.get("dat").get("list"));//返回机器信息list
            }
            JsonNode sourceList = FlashcatKit.sourceList(jsonNode.get("dat").get("access_token").asText());
            if(sourceList!=null&&"".equals(sourceList.get("error").asText())){
                set("sourceList",sourceList.get("data"));//返回数据源list
            }
            set("authorization",jsonNode.get("dat").get("access_token").asText());//返回登入认证信息
        }
        render("index.html");
    }

    /**
     * 返回服务单元状态监控页面
     * */
    @Clear
    public void nodeMonitor() {
        set("chainId",getPara("chainId"));//返回链ID
        set("job",getPara("job"));//返回指定节点
        JsonNode jsonNode = FlashcatKit.flashcatLogin();
        if(jsonNode!=null&&"".equals(jsonNode.get("err").asText())){
            String datasoureId = getFlashCatBlockchainDatasoureId(jsonNode.get("dat").get("access_token").asText());
            set("datasoureId",datasoureId);//返回指定数据源ID
            JsonNode chainJobList = FlashcatKit.chainJobList(datasoureId,jsonNode.get("dat").get("access_token").asText());
            if(chainJobList!=null&&"success".equals(chainJobList.get("status").asText())){
                set("chainJobList",chainJobList.get("data"));//返回机器信息list
                JsonNode data = chainJobList.get("data");
                Set chainIdList = new HashSet<>();
                if (data.isArray()) {
                    for (JsonNode elementNode : data) {
                        chainIdList.add(elementNode.get("chainId"));
                    }
                }
                set("chainIdList",chainIdList);//返回链id集合
            }
            set("authorization",jsonNode.get("dat").get("access_token").asText());//返回登入认证信息
        }
        render("nodeMonitoring.html");
    }

    /**
     * 返回夜莺平台区块链数据源id
     * */
    public String getFlashCatBlockchainDatasoureId(String token){
        String datasoureId="1";//默认为1
        String  Datasoure= PropKit.get("flashcat.blockchainDatasoure");
        JsonNode sourceList = FlashcatKit.sourceList(token);
        if(sourceList!=null&&"".equals(sourceList.get("error").asText())){
            JsonNode dataList = sourceList.get("data");
            for (JsonNode data:dataList) {
                datasoureId=data.get("id").asText();
                if(Datasoure.equals(data.get("name").asText())){
                    return data.get("id").asText();
                }
            }
        }
        return datasoureId;
    }

    /**
     * 查看cpu使用率
     *  ident 机器标识
     *  date  日期
     *  contrastDate 比对日期
     *  startHour  开始时间（小时）
     *  endHour   结束时间（小时）
     *  step   时间颗粒度
     *  datasoureId 数据源id
     *  authorization  认证字符串
     * */
    @Clear
    public void cpuUsageSystem(){
        String ident = getPara("ident");
        String date = getPara("date");
        String contrastDate = getPara("contrastDate");
        String startHour = getPara("startHour");
        String endHour = getPara("endHour");
        String step = getPara("step");
        String datasoureId = getPara("datasoureId");
        String authorization = getPara("authorization");
        JsonNode jsonNode = asr.cpuUsageSystem(ident, date, contrastDate, startHour, endHour, step, datasoureId, authorization);
        renderJson(jsonNode.toString());
    }
    /**
     * 查看内存使用率
     *  ident 机器标识
     *  date  日期
     *  contrastDate 比对日期
     *  startHour  开始时间（小时）
     *  endHour   结束时间（小时）
     *  step   时间颗粒度
     *  datasoureId 数据源id
     *  authorization  认证字符串
     * */
    @Clear
    public void memUsedPercent(){
        String ident = getPara("ident");
        String date = getPara("date");
        String contrastDate = getPara("contrastDate");
        String startHour = getPara("startHour");
        String endHour = getPara("endHour");
        String step = getPara("step");
        String datasoureId = getPara("datasoureId");
        String authorization = getPara("authorization");
        JsonNode jsonNode = asr.memUsedPercent(ident, date, contrastDate, startHour, endHour, step, datasoureId, authorization);
        renderJson(jsonNode.toString());
    }

    /**
     * 查看硬盘使用率
     *  ident 机器标识
     *  date  日期
     *  contrastDate 比对日期
     *  startHour  开始时间（小时）
     *  endHour   结束时间（小时）
     *  step   时间颗粒度
     *  datasoureId 数据源id
     *  authorization  认证字符串
     * */
    @Clear
    public void diskUsedPercent(){
        String ident = getPara("ident");
        String date = getPara("date");
        String contrastDate = getPara("contrastDate");
        String startHour = getPara("startHour");
        String endHour = getPara("endHour");
        String step = getPara("step");
        String datasoureId = getPara("datasoureId");
        String authorization = getPara("authorization");
        JsonNode jsonNode = asr.diskUsedPercent(ident, date, contrastDate, startHour, endHour, step, datasoureId, authorization);
        renderJson(jsonNode.toString());
    }

    /**
     * 查看上行带宽
     *  ident 机器标识
     *  date  日期
     *  contrastDate 比对日期
     *  startHour  开始时间（小时）
     *  endHour   结束时间（小时）
     *  step   时间颗粒度
     *  datasoureId 数据源id
     *  authorization  认证字符串
     * */
    @Clear
    public void netBytesSent(){
        String ident = getPara("ident");
        String date = getPara("date");
        String contrastDate = getPara("contrastDate");
        String startHour = getPara("startHour");
        String endHour = getPara("endHour");
        String step = getPara("step");
        String datasoureId = getPara("datasoureId");
        String authorization = getPara("authorization");
        JsonNode jsonNode = asr.netBytesSent(ident, date, contrastDate, startHour, endHour, step, datasoureId, authorization);
        renderJson(jsonNode.toString());
    }

    /**
     *  区块链高度
     *  chainId 链ID
     *  job  链节点
     *  date  日期
     *  contrastDate 比对日期
     *  startHour  开始时间（小时）
     *  endHour   结束时间（小时）
     *  step   时间颗粒度
     *  datasoureId 数据源id
     *  authorization  认证字符串
     * */
    @Clear
    public void chainmakerBlockCounter(){
        String chainId = getPara("chainId");
        String job = getPara("job");
        String date = getPara("date");
        String contrastDate = getPara("contrastDate");
        String startHour = getPara("startHour");
        String endHour = getPara("endHour");
        String step = getPara("step");
        String datasoureId = getPara("datasoureId");
        String authorization = getPara("authorization");
        ArrayNode jsonNode = asr.chainmakerBlockCounter(chainId,job, date, contrastDate, startHour, endHour, step, datasoureId, authorization);
        renderJson(jsonNode.toString());
    }

    /**
     *  交易池数量
     *  chainId 链ID
     *  job  链节点
     *  date  日期
     *  contrastDate 比对日期
     *  startHour  开始时间（小时）
     *  endHour   结束时间（小时）
     *  step   时间颗粒度
     *  datasoureId 数据源id
     *  authorization  认证字符串
     * */
    @Clear
    public void chainmakerTxPoolSize(){
        String chainId = getPara("chainId");
        String job = getPara("job");
        String date = getPara("date");
        String contrastDate = getPara("contrastDate");
        String startHour = getPara("startHour");
        String endHour = getPara("endHour");
        String step = getPara("step");
        String datasoureId = getPara("datasoureId");
        String authorization = getPara("authorization");
        ArrayNode jsonNode = asr.chainmakerTxPoolSize(chainId,job, date, contrastDate, startHour, endHour, step, datasoureId, authorization);
        renderJson(jsonNode.toString());
    }

    /**
     * 区块链tps
     *  chainId 链ID
     *  job  链节点
     *  date  日期
     *  contrastDate 比对日期
     *  startHour  开始时间（小时）
     *  endHour   结束时间（小时）
     *  step   时间颗粒度
     *  datasoureId 数据源id
     *  authorization  认证字符串
     * */
    @Clear
    public void chainmakerTps(){
        String chainId = getPara("chainId");
        String job = getPara("job");
        String date = getPara("date");
        String contrastDate = getPara("contrastDate");
        String startHour = getPara("startHour");
        String endHour = getPara("endHour");
        String step = getPara("step");
        String datasoureId = getPara("datasoureId");
        String authorization = getPara("authorization");
        ArrayNode jsonNode = asr.chainmakerTps(chainId,job, date, contrastDate, startHour, endHour, step, datasoureId, authorization);
        renderJson(jsonNode.toString());
    }


    /**
     *  * 待打包交易数
     *  chainId 链ID
     *  job  链节点
     *  date  日期
     *  contrastDate 比对日期
     *  startHour  开始时间（小时）
     *  endHour   结束时间（小时）
     *  step   时间颗粒度
     *  datasoureId 数据源id
     *  authorization  认证字符串
     * */
    @Clear
    public void chainmakerTxCounter(){
        String chainId = getPara("chainId");
        String job = getPara("job");
        String date = getPara("date");
        String contrastDate = getPara("contrastDate");
        String startHour = getPara("startHour");
        String endHour = getPara("endHour");
        String step = getPara("step");
        String datasoureId = getPara("datasoureId");
        String authorization = getPara("authorization");
        ArrayNode jsonNode = asr.chainmakerTxCounter(chainId,job, date, contrastDate, startHour, endHour, step, datasoureId, authorization);
        renderJson(jsonNode.toString());
    }

    /**
     *区块链交易类请求数
     *  date  日期
     *  contrastDate 比对日期
     *  startHour  开始时间（小时）
     *  endHour   结束时间（小时）
     *  step   时间颗粒度
     *  datasoureId 数据源id
     *  authorization  认证字符串
     * */
    @Clear
    public void chainmakerInvokeRequest(){
        String chainId = getPara("chainId");
        String job = getPara("job");
        String date = getPara("date");
        String contrastDate = getPara("contrastDate");
        String startHour = getPara("startHour");
        String endHour = getPara("endHour");
        String step = getPara("step");
        String datasoureId = getPara("datasoureId");
        String authorization = getPara("authorization");
        ArrayNode jsonNode = asr.chainmakerInvokeRequest(chainId,job,date, contrastDate, startHour, endHour, step, datasoureId, authorization);
        renderJson(jsonNode.toString());
    }

    /**
     **区块链查询类请求数
     *  chainId 链ID
     *  job  链节点
     *  date  日期
     *  contrastDate 比对日期
     *  startHour  开始时间（小时）
     *  endHour   结束时间（小时）
     *  step   时间颗粒度
     *  datasoureId 数据源id
     *  authorization  认证字符串
     * */
    @Clear
    public void chainmakerQueryRequest(){
        String chainId = getPara("chainId");
        String job = getPara("job");
        String date = getPara("date");
        String contrastDate = getPara("contrastDate");
        String startHour = getPara("startHour");
        String endHour = getPara("endHour");
        String step = getPara("step");
        String datasoureId = getPara("datasoureId");
        String authorization = getPara("authorization");
        ArrayNode jsonNode = asr.chainmakerQueryRequest(chainId,job,date, contrastDate, startHour, endHour, step, datasoureId, authorization);
        renderJson(jsonNode.toString());
    }

    /**
     **区块链平均块大小
     *  chainId 链ID
     *  job  链节点
     *  date  日期
     *  contrastDate 比对日期
     *  startHour  开始时间（小时）
     *  endHour   结束时间（小时）
     *  step   时间颗粒度
     *  datasoureId 数据源id
     *  authorization  认证字符串
     * */
    @Clear
    public void chainmakerAvgBlockSize(){
        String chainId = getPara("chainId");
        String job = getPara("job");
        String date = getPara("date");
        String contrastDate = getPara("contrastDate");
        String startHour = getPara("startHour");
        String endHour = getPara("endHour");
        String step = getPara("step");
        String datasoureId = getPara("datasoureId");
        String authorization = getPara("authorization");
        ArrayNode jsonNode = asr.chainmakerAvgBlockSize(chainId,job,date, contrastDate, startHour, endHour, step, datasoureId, authorization);
        renderJson(jsonNode.toString());
    }

    /**
     **区块链平均出块周期
     *  chainId 链ID
     *  job  链节点
     *  date  日期
     *  contrastDate 比对日期
     *  startHour  开始时间（小时）
     *  endHour   结束时间（小时）
     *  step   时间颗粒度
     *  datasoureId 数据源id
     *  authorization  认证字符串
     * */
    @Clear
    public void chainmakerAvgBlockOutput(){
        String chainId = getPara("chainId");
        String job = getPara("job");
        String date = getPara("date");
        String contrastDate = getPara("contrastDate");
        String startHour = getPara("startHour");
        String endHour = getPara("endHour");
        String step = getPara("step");
        String datasoureId = getPara("datasoureId");
        String authorization = getPara("authorization");
        ArrayNode jsonNode = asr.chainmakerAvgBlockOutput(chainId,job,date, contrastDate, startHour, endHour, step, datasoureId, authorization);
        renderJson(jsonNode.toString());
    }

    /**
     **区块链平均Tps
     *  chainId 链ID
     *  job  链节点
     *  date  日期
     *  contrastDate 比对日期
     *  startHour  开始时间（小时）
     *  endHour   结束时间（小时）
     *  step   时间颗粒度
     *  datasoureId 数据源id
     *  authorization  认证字符串
     * */
    @Clear
    public void chainmakerAvgTps(){
        String chainId = getPara("chainId");
        String job = getPara("job");
        String date = getPara("date");
        String contrastDate = getPara("contrastDate");
        String startHour = getPara("startHour");
        String endHour = getPara("endHour");
        String step = getPara("step");
        String datasoureId = getPara("datasoureId");
        String authorization = getPara("authorization");
        ArrayNode jsonNode = asr.chainmakerAvgTps(chainId,job,date, contrastDate, startHour, endHour, step, datasoureId, authorization);
        renderJson(jsonNode.toString());
    }

}
